// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#include "coin2/exchange/mexc/api_util_v3/auth_key.h"

#include "coin2/base/time.h"

namespace coin2::exchange::mexc::api_util_v3 {

AsioHttpContext* MexcRestAuth::CreateRequestByGetWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  return AsioHttpContext::NewGet(host_, port_, bind_ip_, GetUrl(address, builder), headers_);
}

AsioHttpContext* MexcRestAuth::CreateRequestByPostWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  return AsioHttpContext::NewPost(host_, port_, bind_ip_, GetUrl(address, builder), headers_, "");
}

AsioHttpContext* MexcRestAuth::CreateRequestByDeleteWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  return AsioHttpContext::NewDelete(host_, port_, bind_ip_, GetUrl(address, builder), headers_);
}

AsioHttpContext* MexcRestAuth::CreateRequestByPutWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  return AsioHttpContext::NewPut(host_, port_, bind_ip_, GetUrl(address, builder), headers_, "");
}

std::string MexcRestAuth::GetUrl(const std::string& address, UrlParamsBuilder* builder) {
  std::string post_body = builder->ComputePostBody();
  CHECK(post_body.empty());

  builder->PutGet("timestamp", GetCurrentTimestamp() / 1000000);

  std::string signature = GetSignature(builder->ComputeQueryString());

  builder->PutGet("signature", signature);

  return address + "?" + builder->ComputeQueryString();
}

}  // namespace coin2::exchange::mexc::api_util_v3
